<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * 这个模块是数据逻辑的一些助手函数，使用的时候先load->library()
*/

if(!function_exists("is_friend")){
	function is_friend($uid1, $uid2)
	{
		$CI	= &get_instance();
		$query = $CI->db->get_where("userfriends", array("uid"=>$uid1, "friendUid"=>$uid2));
		return $query->num_rows() > 0;
	}
}

if(!function_exists("mysql_datetime"))
{
	function mysql_datetime()
	{
		date_default_timezone_set("PRC");
		return date("Y-m-d H:i:s");
	}
}

if(!function_exists("get_name_by_uid")){
	function get_name_by_uid($uid)
	{
		$CI	= &get_instance();
		$query = $CI->db->get_where("userbaseinfo", array("uid"=>$uid));

		if(!$query->num_rows())
		{
			return "unknown";
		}

		$row = array_pop($query->result());

		return $row->lastName.$row->firstName;
	}
}

if(!function_exists("get_nickname_by_uid")){
	function get_nickname_by_uid($uid)
	{
		$CI	= &get_instance();
		$query = $CI->db->get_where("userlogin", array("uid"=>$uid));

		if(!$query->num_rows())
		{
			return "unknown";
		}

		$row = array_pop($query->result());

		return $row->nickname;
	}
}

/*
 * 将数据库字段翻译成显示名
*/
if(!function_exists("translate_field")){
	function  translate_field($f)
	{
		static $map= null;

		if(!$map)
		{
			$CI = &get_instance();
			$query = $CI->db->get("healthfieldmeta");
			for($i = 0; $i < $query->num_rows(); $i ++)
			{
				$row = $query->row_array($i);
				$field = $row["columnName"];
				$desc = $row["description"];
				$map[$field] = $desc;
			}
		}
		
		return $map[$f];
	}
}

/*
 * 过滤需要显示的健康数据字段
*/
if(!function_exists("valid_field")){
	function valid_field($f)
	{
		static $fields = null;
		if(!$fields)
		{
			$fields = array();
			$CI = &get_instance();
			$CI->db->select("columnName");
			$query = $CI->db->get("healthfieldmeta");
			for($i = 0; $i < $query->num_rows(); $i++ )
			{
				$row = $query->row_array($i);
				$fields[] = current($row);;
			}
		}


		return in_array($f, $fields);
	}
}

if(!function_exists("get_health_info_fields"))
{
	function get_health_info_fields()
	{
		$CI = &get_instance();
		$fields = $CI->db->list_fields('userhealthinfo');
		return $fields;
	}
}

if(!function_exists("get_recent_health_info")){
	function get_recent_health_info($uid)
	{
		$CI = &get_instance();
		$CI->db->select_max("healthInfoId");
		$query = $CI->db->get_where("userhealthinfo",array(
				"uid"=>$uid));
		$res = $query->result();
		$maxId = $res[0]->healthInfoId;


		if(!$maxId)
			return array();

		$query = $CI->db->get_where("userhealthinfo", array(
				"healthInfoId"=>$maxId
		));
		$record = $query->row_array(0);


		return $record;
	}
}

/*
 * 安全地取得healthInfo的各个字段的值
*/
if(! function_exists("get_value_of_field")){
	function get_value_of_field($field, $health_info)
	{
		if(array_key_exists($field, $health_info))
			return $health_info[$field];

		return "";
	}
}

/*
 * 将类型码映射为类型名称
*
*/
if(! function_exists("get_health_info_type"))
{
	function get_health_info_type($type_code)
	{
		$map = get_health_info_type_map();
		if(!array_key_exists($type_code, $map))
			return "";


		return $map[$type_code];
	}
}

/*
 * 获取字段类型和名称的映射
*
*/

if(! function_exists("get_health_info_type_map"))
{
	function get_health_info_type_map()
	{
		$map = array(
				"0"=>"BOOL",
				"1"=>"FLOAT",
				"2"=>"INT",
				//"3"=>"VARCHAR",
		);


		return $map;
	}
}


if(! function_exists("get_symptom_map"))
{
	function get_symptom_map()
	{
		$symptom_map = array(
				"总胆固醇临界：临界超标", //0
				"总胆固醇临界：超标",	//1
				"低密度脂蛋白：临界超标",//2
				"低密度脂蛋白：超标",//3
				"高密度脂蛋白：过低",//4
				"甘油三酯：临界超标",//5
				"甘油三酯：超标",//6
				"血糖：临界糖尿病",//7
				"血糖：糖尿病",//8
				"血糖 （未禁食）：超标",//9
				"血压：临界高血压",//10
				"血压：一阶高血压",//11
				"血压：二阶高血压",//12
				"消瘦",//13
				"超重",//14
				"肥胖",//15
				"困",//16
				"乏",//17
				"晕",//18
				"闷",//19
				
		);
		
		return $symptom_map;
	}
}

if(!function_exists("get_symptom_name_by")){
	function get_symptom_name_by($symptom_id)
	{
		if($symptom_id < 0)
			return "";
		$map = get_symptom_map();
		return $map[$symptom_id];
	}
}

if(!function_exists("get_symptom_id_by"))
{
	function get_symptom_id_by($symptom_name)
	{
		$map = get_symptom_map();
		foreach($map as $key=>$value)
		{
			if($value == $symptom_name)
				return $key;
		}
		return -1;
	}
}

if(!function_exists("get_user_nick_name"))
{
	function get_user_nick_name($uid)
	{
		$CI = &get_instance();
		$CI->db->where("uid",$uid);
		$query = $CI->db->get("userlogin");
		$row = $query->row_array();

		return $row["nickname"];
	}
}

if(!function_exists("get_user_full_name"))
{
	function get_user_full_name($uid)
	{
		$CI = &get_instance();
		$query = $CI->db->query("select concat(lastName, firstName) as fullName from userbaseinfo where uid = $uid;");
		$row = $query->row_array();

		return $row["fullName"];
	}
}

if(!function_exists("field_shared")){
	function field_shared($uid, $field)
	{
		$CI = &get_instance();
		$CI->db->where("uid", $uid);
		$query = $CI->db->get("usershare");
		if(!$query->num_rows())
			return false;
		
		$share_info = $query->row_array(0);
		
		return array_key_exists($field, $share_info) && $share_info[$field];
	}
}

if(!function_exists("get_unsuppressible_field"))
{
	function get_unsuppressible_field()
	{
		$set = array(
				"height",
				"weight",
				"waistline",
				"bloodPressureHigh",
				"bloodPressureLow",
				"totalCholesterol",
				"highDensityLipoprotein",
				"lowDensityLipoprotein",
				"triglyceride",
				"fastingBloodSugar",
				"normalBloodSugar",
				"smoking",
				"drinkingWeekFreq",
				"doseOfDrink",
				"sportsWeekFreq",
				"sportsDuration",
				"pressure"
				);
		
		return $set;
	}
}

if(!function_exists("can_delete_field"))
{
	function can_delete_field($field)
	{
		$set = get_unsuppressible_field();
		return !in_array($field, $set);
	}
}

if(!function_exists("get_symptom_relation_map"))
{
	function get_symptom_relation_map()
	{
		$map = array(
				"总胆固醇临界：临界超标"=>1,
				"总胆固醇临界：超标"=>1,
				"低密度脂蛋白：临界超标"=>2,
				"低密度脂蛋白：超标"=>2,
				"甘油三酯：临界超标"=>3,
				"甘油三酯：超标"=>3,
				"血糖：临界糖尿病"=>4,
				"血糖：糖尿病"=>4,
				"血压：临界高血压"=>5,
				"血压：一阶高血压"=>5,
				"血压：二阶高血压"=>5,
				"消瘦"=>6,
				"超重"=>6,
				"肥胖"=>6,
				);
		
		return $map;
	}

}

if(!function_exists("get_conflict_symptom"))
{
	function get_conflict_symptom($symptom_name)
	{
		$map = get_symptom_relation_map();
		$my_code = $map[$symptom_name];
		$result = array();
		foreach($map as $name=>$code)
		{
			if($name == $symptom_name)
				continue;
			
			if($code==$my_code)
				$result[] = $name;
		}
		
		return $result;
	}
}

?>